package cn.e3mall.sso.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e3mall.common.util.CookieUtils;
import cn.e3mall.common.util.E3Result;
import cn.e3mall.sso.service.LoginService;

@Controller
public class LoginController {

	@Autowired
	private LoginService loginService;
	@Value("${TOKEN_KEY}")
	private String TOKEN_KEY;
	
	@RequestMapping("/page/login")
	public String showLogin(String redirect,Model m){
		m.addAttribute("redirect", redirect);  
		return "login";
	}
	
	@RequestMapping(value="/user/login",method=RequestMethod.POST)
	@ResponseBody
	public E3Result login(String username,String password, HttpServletRequest request, HttpServletResponse response){
		E3Result result = loginService.userLogin(username, password);
		//判断是否登陆成功，成功就将token写入cookie
		if(result.getStatus()==200){
			String token = result.getData().toString();
			//如果登陆成功需要吧token写入cookie中
			CookieUtils.setCookie(request, response, TOKEN_KEY, token);
		}
		//返回结果
		return result ;
	} 
}
